package com.multicampus.simplewiki.control;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.multicampus.simplewiki.ctrl.DocumentManager;
import com.multicampus.simplewiki.model.Document;
import com.multicampus.simplewiki.model.User;

public class UpdatePageController extends SimpleFormController {
	private DocumentManager documentManager;

	public void setDocumentManager(DocumentManager documentManager) {
		this.documentManager = documentManager;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		Document document = (Document) command;
		User user = (User)request.getSession().getAttribute("user");
		
		System.out.println(document.getId());
		document.setAuthor(user);

		documentManager.updateDocument(document, request
				.getParameter("parentId"));
		
		String url = "redirect:/view_page.do?id=" + document.getId();
		
		return new ModelAndView(url, "document", document);
	}
}
